#include "../include/list_queue.h"
#include "../include/msg.h"

/*****************************************************************************
 *
 * 链表相关
 *
 ****************************************************************************/

list_t pcclient_list_head;
list_t device_list_head;

volatile sig_atomic_t timeout_occurred = 0;

// 获取链表头节点
list_t *get_pcclient_list_head()
{
	return &pcclient_list_head;
}

list_t *get_device_list_head()
{
	return &device_list_head;
}

void list_init(list_t *head)
{
	memset(head, 0, sizeof(list_t));
	head->next = NULL;
}

// 头插
int list_add_head(list_t *head, list_t *node)
{
	if (node == NULL || head == NULL)
	{
		ERR_LOG("node is NULL");
	}

	// list_t *new = (list_t *)malloc(sizeof(list_t));
	// *new = *node;
	node->next = head->next;
	head->next = node;

	return 0;
}

// 尾插
int list_add_tail(list_t *head, list_t *node)
{
	if (node == NULL || head == NULL)
	{
		ERR_LOG("error");
	}

	list_t *temp = head;
	while (temp->next != NULL)
	{
		temp = temp->next;
	}

	temp->next = node;
	node->next = NULL;

	return 0;
}

// 头删
int list_del_head(list_t *head)
{
	if (head == NULL)
	{
		ERR_LOG("head is NULL");
	}

	if (head->next != NULL)
	{
		list_t *del = head->next;
		head->next = del->next;
		free(del);
	}

	return 0;
}

// 指定位置删除
int list_del_node(list_t *head, int fd)
{
	if (head == NULL)
	{
		ERR_LOG("head is NULL");
	}

	list_t *temp = head;
	while (temp->next != NULL)
	{
		if (temp->next->client_sockfd == fd)
		{
			list_t *del = temp->next;
			temp->next = del->next;
			free(del);
			return 0;
		}
		temp = temp->next;
	}

	printf("not find node %d\n", fd);
}

/**
 * 遍历链表查找指定的verify
 * 返回0成功找到，返回-1没有找到
 */
int list_find_verify(list_t *head, int verify)
{
	if (head == NULL)
		ERR_LOG("head is NULL");

	list_t *temp = head->next;
	while (temp != NULL)
	{
		if (temp->verify == verify)
		{
			return 0;
		}
		temp = temp->next;
	}

	return -1;
}

/**
 * 遍历链表查找指定的devnum
 * 返回0成功找到，返回-1没有找到
 */
int list_find_devnum(list_t *head, char *devnum)
{
	if (head == NULL)
		ERR_LOG("head is NULL");

	list_t *temp = head->next;
	while (temp != NULL)
	{
		if (strcmp(temp->devnum, devnum) == 0)
		{
			return 0;
		}
		temp = temp->next;
	}

	return -1;
}

// 链表遍历
int list_traverse(list_t *head)
{
	if (head == NULL)
		ERR_LOG("head is NULL");

	list_t *temp = head->next;
	printf("-------------------------------------------------------------\n");
	while (temp != NULL)
	{
		printf("[%d:%d] ", temp->client_sockfd, temp->verify);
		temp = temp->next;
	}
	printf("\n");
	printf("-------------------------------------------------------------\n");

	return 0;
}

/*****************************************************************************
 *
 * 消息队列相关
 *
 ****************************************************************************/

int qt_to_dev_msgid = 0;
int dev_to_qt_msgid = 0;

int *get_qtod_queue()
{
	return &qt_to_dev_msgid;
}

int *get_dtoq_queue()
{
	return &dev_to_qt_msgid;
}

/**
 * 初始化消息队列
 * 返回 0成功  -1失败
 */
int msg_queue_init(int *msgid, char ch)
{
	key_t my_key = 0;
	if (-1 == (my_key = ftok("/home", ch)))
		ERR_LOG("ftok error");

	*msgid = msgget(my_key, IPC_CREAT | 0666);
	if (-1 == *msgid)
		ERR_LOG("msgget error");

	return 0;
}

/**
 * 向消息队列中插入数据
 * 返回0 插入成功  1插入失败
 */
int msg_send_to_queue(int *msgid, msg_queue_t *clientmsg)
{
	int ret;
	if (clientmsg == NULL)
		ERR_LOG("msg_send_to_queue input");

	msg_queue_t queue_msg;
	memset(&queue_msg, 0, sizeof(msg_queue_t));

	// 将msg中的消息读取到消息队列结构体中
	strcpy(queue_msg.jsonbuf, clientmsg->jsonbuf);
	queue_msg.msgtype = clientmsg->msgtype;

	if (-1 == msgsnd(*msgid, &queue_msg, sizeof(queue_msg) - sizeof(queue_msg.msgtype), 0))
		ERR_LOG("msgsnd error");

	return 0;
}

int msg_recv_to_queue(int *msgid, msg_queue_t *clientmsg, long msgtype)
{
	int ret = 0;
	timeout_occurred = 0;
	if (clientmsg == NULL)
		ERR_LOG("msg_send_to_queue input");

	ssize_t bytes_read;
	bytes_read = msgrcv(*msgid, clientmsg, sizeof(clientmsg->jsonbuf), msgtype, 0);
	if (bytes_read == -1)
	{
		ERR_LOG("msgrecv error");
	}

	return 0;
}
